<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
class Common
{
    // 格式化输出数组
    static function dump($html, $var = "", $error = FALSE) {
        if (ini_get ( "html_errors" )) {
            $content = "<pre>\n";
            if ($var != "") {
                $content .= "<strong>" . $var . " :</strong>\n";
            }
            $content .= htmlspecialchars ( print_r ( $html, TRUE ) );
            $content .= "\n</pre>\n";
        } else {
            $content = $var . " :\n" . print_r ( $html, TRUE );
        }
        if ($error) {
            return $content;
        }
        echo $content;
    }

    // 获取随机数
    static function random($num) {
        mt_srand ( ( double ) microtime () * 100000000 );
        $num = rand ( 10000000, 90000000 );
        return $num;
    }

    // 获取ip
    static function getip() {
        $ip = $_SERVER['REMOTE_ADDR'];
        if (isset($_SERVER['HTTP_X_REAL_FORWARDED_FOR']) && preg_match('/^([0-9]{1,3}.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_REAL_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_REAL_FORWARDED_FOR'];
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match('/^([0-9]{1,3}.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
        return $ip;
    }

    /**
     * 加密函数
     * Enter description here ...
     * @param string $encrypt
     * @param string $mc_key
     */
    static function mc_encrypt($encrypt, $mc_key) {
        $iv = mcrypt_create_iv ( mcrypt_get_iv_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND );
        $passcrypt = trim ( mcrypt_encrypt ( MCRYPT_RIJNDAEL_256, $mc_key, trim ( $encrypt ), MCRYPT_MODE_ECB, $iv ) );
        $encode = base64_encode ( $passcrypt );
        return $encode;
    }
    /**
     * 解密函数
     * Enter description here ...
     * @param string $decrypt
     * @param string $mc_key
     */
    static function mc_decrypt($decrypt, $mc_key) {
        $decoded = base64_decode ( $decrypt );
        $iv = mcrypt_create_iv ( mcrypt_get_iv_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND );
        $decrypted = trim ( mcrypt_decrypt ( MCRYPT_RIJNDAEL_256, $mc_key, trim ( $decoded ), MCRYPT_MODE_ECB, $iv ) );
        return $decrypted;
    }

    // 生成短信验证码
    static function smsCode($len=6){
        $code = '';
        for($i = 0; $i<$len; $i++){
            $code .= rand(0,9);
        }
        return $code;
    }

    // 获取最后执行的sql语句
    static function last_query(){
        $sql = DB::getQueryLog();
        $query = end($sql);
        return $query['query'];

    }

    // redis
    static function redis(){
        $redis = $_ENV['redis'];
        return $redis;
    }

    // CURL get
    static function http_get($url, $ctime = 3, $timeout = 4) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $ctime);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    // CURL post
    static function http_post($url, $post_array = array(), $ctime = 3, $timeout = 4) {
        if (!is_array($post_array))
            return FALSE;
        $post_data = '';
        foreach ($post_array as $key => $var) {
            $post_data .= $key . '=' . urlencode($var) . '&';
        }
        $post_data = substr($post_data, 0, -1);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $ctime);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}
